package com.quanyan.place.component;


import com.google.gson.Gson;
import com.quanyan.mq.client.QcMsgClient;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.mq.client.reqeust.SendCallbackListener;
import com.quanyan.mq.client.response.MqResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;

/**
 * Created by jian.zhou on 2016/7/15.
 */
@Service
public class MqSportsClient implements QcMsgClient {

    @Autowired
    private MqSportProducer mqProducer;

    private Logger logger = LoggerFactory.getLogger(MqSportsClient.class);
    @Nonnull
    @Override
    public MqResponse syncSend(@Nonnull MqRequestMsg mqRequestMsg) {
        return mqProducer.syncSend(mqRequestMsg);
    }

    @Override
    public void asyncSend(@Nonnull MqRequestMsg mqRequestMsg) {
        logger.warn("SEND TO MQ : DATA : {}", new Gson().toJson(mqRequestMsg).toString());
        mqProducer.asyncSend(mqRequestMsg, new SendCallbackListener() {

            @Override
            public void onSuccess(String sendResult) {
                logger.warn("SEND TO MQ : RESULT : {}", sendResult);
            }

            @Override
            public void onException(Throwable e) {
                logger.warn("SEND TO MQ EXCEPTION : {}", e.getMessage());
            }
        });
    }
}


